#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#include "string.h"
long long int String_To_Int(char *string)
{
    int Length = 0;
    long long int  Temp_Value = 0;
    int i = 0;
    long long int Times_16 = 1;

    //Caculate the length of the string
    Length = strlen(string);
    printf("The Length is %d\r\n",Length);
    
    //Convert every character to a value
    for( i = Length - 1 ; i >= 0;i--)
    {
       if((string[i] >= 0x30) && (string[i] <= 0x39))
       {
           Temp_Value += ((string[i] - 0x30)*Times_16);
           Times_16 *= 16;
         
       }
       else if((string[i] >= 0x41) &&( string[i] <= 0x46))
       {
           Temp_Value += ((string[i] - (0x41 - 0xa)) * Times_16);
           Times_16 *= 16;
       }
       else if((string[i] >= 0x61) &&( string[i] <= 0x66))
       {
           Temp_Value += ((string[i] - (0x61 - 0xa)) * Times_16);
           Times_16 *= 16;
       }
       else
       { 
           printf("Wrong number!\r\n");
           return -1;
       }
       printf(" string[i] is %c and Temp_Value is %llx \r\n",string[i],Temp_Value);
    } 
    printf("Temp_Value is %llx\r\n",Temp_Value);

    return Temp_Value;
}
int main(int argc, char *argv[])
{
    int *x = 0;
    long long int Temp_Value = 0;
    int value = 0;
    if(argc > 1)
    {
        printf(" argv[1] = %s\r\n",argv[1]);
        Temp_Value = String_To_Int(argv[1]);
        if((Temp_Value != -1) && (Temp_Value != 0))
        {
            value = Temp_Value & 0xf;
            printf("Your value should be 0x%x\r\n",value);
        }
    }
    else
    {
        printf("Need more than one argv.\r\n");
        return 0;
    }

        return 0;
} 
